﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using WCYCReader.Utility;
using System.Windows.Media.Imaging;

namespace WCYCReader.UserControls
{
    public partial class ReaderPageTopControl : UserControl
    {
        
        private bool TopPanel_Begin;
        private bool TopPanel_Night;

        public ReaderPageTopControl()
        {
            this.InitializeComponent();
            this.UpdateIcon();
        }

        private void AddMarker_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.PopupTopPanel(ReaderPageMenuPanel.RAddMarkerPanel);
        }

        private void BookChapter_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.PopupTopPanel(ReaderPageMenuPanel.RBookChapterPanel);
        }

        private void ForBegin_Icon(bool begin)
        {
            if (begin)
            {
                this.ForBeginMenuImage.Source = new BitmapImage(new Uri("/Resource/Images/begin1.png", UriKind.Relative));
                this.ForBeginMenuText.Text = "取消固定";
                return;
            }
            this.ForBeginMenuImage.Source = new BitmapImage(new Uri("/Resource/Images/begin2.png", UriKind.Relative));
            this.ForBeginMenuText.Text = "固定到开始";
        }

        private void ForBegin_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.ForBegin_Icon(!this.IsForBegin());
            this.PopupTopPanel(ReaderPageMenuPanel.RForBeginPanel);
        }



        private bool IsForBegin()
        {
            if (ShellTile.ActiveTiles.FirstOrDefault<ShellTile>((ShellTile x) => x.NavigationUri.ToString().Contains(App.ReaderFileName)) != null)
            {
                this.TopPanel_Begin = true;
            }
            else
            {
                this.TopPanel_Begin = false;
            }
            return this.TopPanel_Begin;
        }

        private void Night_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.TopPanel_Night = (bool)SettingHelper.GetValueFromSettings("BookReader_NightMode");
            this.NightMode_Icon(this.TopPanel_Night = !this.TopPanel_Night);
            SettingHelper.SaveInToSettings("BookReader_NightMode", this.TopPanel_Night);
            this.PopupTopPanel(ReaderPageMenuPanel.RNightPanel);
        }

        private void NightMode_Icon(bool night)
        {
            if (night)
            {
                this.MoonOrSunMenuImage.Source = new BitmapImage(new Uri("/Resource/Images/sun.png", UriKind.Relative));
                this.MoonOrSunMenuText.Text = "正常模式";
                return;
            }
            this.MoonOrSunMenuImage.Source = new BitmapImage(new Uri("/Resource/Images/night.png", UriKind.Relative));
            this.MoonOrSunMenuText.Text = "夜间模式";
        }

        public void UpdateIcon()
        {
            this.TopPanel_Night = (bool)SettingHelper.GetValueFromSettings("BookReader_NightMode");
            this.NightMode_Icon(this.TopPanel_Night);
            this.ForBegin_Icon(this.IsForBegin());
        }
    }
}
